package com.xiongda.controller;

import com.xiongda.common.Const;
import com.xiongda.common.ServerResponse;
import com.xiongda.pojo.CommentWithBLOBs;
import com.xiongda.pojo.User;
import com.xiongda.service.CommentService;
import com.xiongda.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 评论模块
 *
 * @author 缪琼
 */
@RestController
@RequestMapping(value = "/comment")
public class CommentController {

    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;

    /**
     * 获取一篇博客底下所有的评论
     * @param pageNo1
     * @param pageNo2
     * @param blogId
     * @param session
     * @return
     */
    @RequestMapping(value = "/getcomment", method = RequestMethod.GET)
    public ServerResponse getComment(@RequestParam(value = "page_no_outer") int pageNo1,
                                     @RequestParam(value = "page_no_inner") int pageNo2,
                                     @RequestParam(value = "blog_id") Long blogId,
                                     HttpSession session) {
        return commentService.getComment(pageNo1, pageNo2, blogId);
    }

    /**
     * 获取一篇博客底下所有的热评
     * @param pageNo
     * @param blogId
     * @param session
     * @return
     */
    @RequestMapping(value = "/gethotcomment", method = RequestMethod.GET)
    public ServerResponse getHotComment(@RequestParam(value = "page_no") int pageNo,
                                        @RequestParam(value = "blog_id") Long blogId,
                                        HttpSession session) {
        return commentService.getHotComment(pageNo, blogId);
    }

    /**
     * 保存评论
     * @param commentWithBLOBs
     * @param session
     * @return
     */
    @RequestMapping(value = "/savecomment", method = RequestMethod.POST)
    public ServerResponse saveComment(@RequestBody CommentWithBLOBs commentWithBLOBs, HttpSession session) {
        User user = userService.getUser(commentWithBLOBs.getUserId());
        if (user == null)
            return ServerResponse.createByErrorMessage("用户不存在");
        user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorMessage("用户未登录");
        return commentService.saveComment(commentWithBLOBs);
    }

    /**
     * 删除评论
     * @param id
     * @param session
     * @return
     */
    @RequestMapping(value = "/deletecomment", method = RequestMethod.DELETE)
    public ServerResponse deleteComment(@RequestParam(value = "id") Long id, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorMessage("用户未登录");
        Long userId = user.getId();
        return commentService.deleteComment(id, userId);
    }
}
